import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:zego_express_screen_capture/xx/srt_transform.dart';

class SrtAnimationPage extends StatefulWidget {
  const SrtAnimationPage({Key? key, required this.list}) : super(key: key);

  final List<SrtModel> list;

  @override
  _SrtAnimationPageState createState() => _SrtAnimationPageState();
}

class _SrtAnimationPageState extends State<SrtAnimationPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      lowerBound: 0,
      upperBound: 10,
      duration: const Duration(seconds: 10),
      vsync: this,
    )..repeat(reverse: false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Xxxxx"),
      ),
      body: CustomPaint(
        // size：当child为null时，代表默认绘制区域大小，如果有child则忽略此参数，画布尺寸则为child尺寸
        size: Size(MediaQuery.of(context).size.width, 5),
        painter: PicManPainter(angle: _controller),
      ),
    );
  }
}

class PicManPainter extends CustomPainter {
  Paint _paint = Paint();
  final Animation<double> angle;
  final Color color; // 颜色
  PicManPainter({this.color = Colors.yellowAccent, required this.angle})
      : super(repaint: angle);

  final paragraphStyle = ui.ParagraphStyle(
    fontSize: 14,
    fontWeight: FontWeight.bold,
    textDirection: TextDirection.ltr,
  );

  @override
  void paint(Canvas canvas, Size size) {
    // canvas.clipRect(Offset.zero & size); //剪切画布
    // final double radius = size.width / 2;
    // canvas.translate(radius, size.height / 2);
    _drawText(canvas, size);
    // xxx(canvas, size);
  }

  void _drawText(Canvas canvas, size) {
    /// 2.根据 ParagraphStyle 生成 ParagraphBuilder
    final paragraphBuilder = ui.ParagraphBuilder(paragraphStyle);

    /// 3.添加样式和文字
    paragraphBuilder
      ..pushStyle(ui.TextStyle(color: Colors.black, fontSize: 16))
      ..addText('黑色文字');


    /// 4.通过 build 取到 Paragraph
    final paragraph = paragraphBuilder.build();

    /// 5.根据宽高进行布局layout
    paragraph.layout(ui.ParagraphConstraints(width: 200));

    /// 6.绘制
    canvas.drawParagraph(paragraph, const Offset(50, 50));
    canvas.translate(50, 20);
    // canvas.save();
    canvas.scale(0.5 +angle.value%4);
    canvas.rotate(pi / 2*angle.value%4);
    canvas.drawParagraph(paragraph, const Offset(30, 50));

    // canvas.restore();
  }

  void xxx(Canvas canvas, size) {
    final textPainter = TextPainter()
      ..text = const TextSpan(
          text: '可多种不同效果的字体来支持富文本',
          style: TextStyle(color: Colors.white, fontSize: 20))
      ..textDirection = TextDirection.ltr

      /// 可以传入minWidth、maxWidth来限制宽度，若不传文字会绘制在一行
      ..layout(maxWidth: 250);

    /// 绘制矩形框，在文字绘制前可通过textPainter.width和textPainter.height来获取文字绘制的尺寸
    canvas.drawRect(
      Rect.fromLTWH(50, 50, textPainter.width, textPainter.height),
      Paint()..color = Colors.black45,
    );

    /// 绘制文字
    textPainter.paint(canvas, const Offset(50, 50));
  }

  @override
  bool shouldRepaint(covariant PicManPainter oldDelegate) {
    print('-----shouldRepaint---------');
    return false;
  }
}
